//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.hs.demo.mina;

import com.alibaba.fastjson.JSONObject;
import com.hs.demo.entity.TbDetectionRl;
import com.hs.demo.mapper.TbDetectionRlMapper;
import com.hs.demo.utils.DateUtilMy;
import com.hs.demo.utils.HttpUtils;
import com.hs.demo.utils.NumberUtils;
import com.hs.demo.vo.TbDetectionRlVO;
import com.mysql.cj.util.StringUtils;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SenMessageService {
    private static final Logger log = LoggerFactory.getLogger(SenMessageService.class);
    @Autowired
    TbDetectionRlMapper detectionRlMapper;
    public void insert(String substring) {
        String[] split = substring.split(",");
//        System.out.print(Arrays.asList(split).toString());
        String[] otherIDS = substring.split("OtherID");
        TbDetectionRl detectionRl = new TbDetectionRl();
        //按 7:00 到 7：00 生成日期
        String erpDate = DateUtilMy.getErpD(DateUtilMy.getForMatCutDay(0));
        Calendar now = Calendar.getInstance();
        int hour = now.get(Calendar.HOUR_OF_DAY);
        log.info("现在时间小时为: " + now.get(Calendar.HOUR_OF_DAY));
        if(hour < 7){
            erpDate = DateUtilMy.getErpD(DateUtilMy.getForMatCutDay(-1));
        }
        int i;
//          "3-1-3-A1"; 字符串
        for(i = 0; i < split.length; i++) {
            if (!StringUtils.isNullOrEmpty(split[i]) && split[i].length() > 4 && split[i].split("-").length > 2) {
                log.info("车间 - 炉号 - 炉次号 - A熔炼 /B球化 第几次测：" + split[i]);
                String[] split1 = split[i].split("-");
                detectionRl.setPlant(split1[0]);
                detectionRl.setFurnaceNum(split1[1]);
                String lch = NumberUtils.int2String(Integer.valueOf(split1[2]));
                detectionRl.setMeltNum(erpDate + "-" + split1[1] + "-" + lch);
                detectionRl.setPackNum(split1[3]);
            }

            if (split[i].equals("C")) {
                detectionRl.setC((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Ce")) {
                detectionRl.setCe((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Cr")) {
                detectionRl.setCr((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Cu")) {
                detectionRl.setCu((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Fe")) {
                detectionRl.setFe((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Mg")) {
                detectionRl.setMg((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Mn")) {
                detectionRl.setMn((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Mo")) {
                detectionRl.setMo((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Ni")) {
                detectionRl.setNi((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("P")) {
                detectionRl.setP((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("S")) {
                detectionRl.setS((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Sb")) {
                detectionRl.setSb((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Si")) {
                detectionRl.setSi((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Sn")) {
                detectionRl.setSn((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("Ti")) {
                detectionRl.setTi((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("V")) {
                detectionRl.setV((new BigDecimal(split[i + 1])).setScale(4, 0));
            } else if (split[i].equals("SampleID")) {
                detectionRl.setPzgg(split[i + 1]);
            } else if (split[i].equals("Worker")) {
                if (StringUtils.isNullOrEmpty(split[i + 1])) {
                    detectionRl.setInspector(split[i + 1]);
                }
            } else if (split[i].equals("OtherID") && otherIDS[1].length() > 1 && !StringUtils.isNullOrEmpty(split[i + 1])) {
                detectionRl.setU8code(split[i + 1]);
            }
        }

        detectionRl.setRemark("plc");
        detectionRl.setCreateTime(new Date());
        System.out.printf(detectionRl.toString());
        if(detectionRl.getPlant() == null){
            log.info("无车间炉号信息，打样数据");
        }else{
            if(detectionRl.getPlant().equals("1") && !StringUtils.isNullOrEmpty(detectionRl.getMeltNum())){
                i = this.detectionRlMapper.insertTbDetectionRl(detectionRl);
                Long id = detectionRl.getId();
                log.info("写入铸一质检数据" + i + "条" + System.currentTimeMillis());
                log.info("推送质检数据开始id:" + id);
                TbDetectionRlVO tbDetectionRl = detectionRlMapper.selectTbDetectionRlByIdCondition(id);
                System.out.println("http推送数据为：" + JSONObject.toJSON(tbDetectionRl));
                HttpUtils.sendPostJson("http://221.214.27.228:6271/",JSONObject.toJSON(tbDetectionRl).toString());


            }else{
                log.info("铸三数据脏数据");
            }
        }

    }
    public static void main(String[] args) {
        String ss = "3-1-3-A";
        String erpDate = DateUtilMy.getErpD(DateUtilMy.getForMatCutDay(0));
        System.out.println(erpDate);
        if (ss.length() > 4 && ss.split("-").length > 2) {
            String[] split1 = ss.split("-");
            System.out.println(split1[0]);
            System.out.println(split1[1]);
            System.out.println(split1[2]);
            System.out.println(split1[3]);
            System.out.println(erpDate + "-" + split1[1] + "-" + NumberUtils.int2String(Integer.valueOf(split1[2])));
        }

    }
}
